<template>
  <div class="login">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      status-icon
    >
      <div class="login-header">
        <h3 class="title">{{ title }}</h3>
        <p class="subtitle">欢迎回来，请登录您的账户</p>
      </div>

      <!-- 用户名 -->
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          placeholder="请输入账号"
          clearable
          class="custom-input"
        >
          <template #prefix>
            <svg-icon icon-class="user" class="input-icon" />
          </template>
        </el-input>
      </el-form-item>

      <!-- 密码 -->
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          placeholder="请输入密码"
          clearable
          show-password
          class="custom-input"
          @keyup.enter.native="handleLogin"
        >
          <template #prefix>
            <svg-icon icon-class="password" class="input-icon" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <div class="code-container">
          <el-input
            v-model="loginForm.code"
            placeholder="请输入验证码"
            class="custom-input code-input"
            clearable
          >
            <template #prefix>
              <el-icon class="input-icon"><Lock /></el-icon>
            </template>
          </el-input>

          <div class="code-image-wrapper" @click="getCode">
            <img class="login-code-img" :src="codeUrl" alt="验证码" />
          </div>
        </div>
      </el-form-item>


      <!-- 记住密码 -->
      <el-form-item class="remember-me">
        <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
      </el-form-item>

      <!-- 登录按钮 -->
      <el-form-item>
        <el-button
          :loading="loading"
          type="primary"
          class="login-btn"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>

      <!-- 注册链接 -->
      <div class="register-link" v-if="register">
        <router-link class="link-type" :to="'/register'">立即注册</router-link>
      </div>
    </el-form>

    <!-- 页脚 -->
    <div class="el-login-footer">
      <span></span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: false,
      // 注册开关
      register: true,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path:  "/paipaiwu/home" }).catch(()=>{})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

.login {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5d0fe, #c084fc);
  padding: 20px;

  &::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url("../assets/images/img6.png");
    background-size: cover;
    opacity:1;
    z-index: 0;
  }

  .login-form {
    position: relative; // 确保表单在伪元素上方
    z-index: 1;
    width: 100%;
    max-width: 420px;
    padding: 40px 30px;
    background: transparent; /* 改为完全透明 */
    border-radius: 20px;


    .login-header {
      text-align: center;
      margin-bottom: 30px;


      .title {
        margin-bottom: 8px;
        font-size: 28px;
        color: #840ad0;
        font-weight: bold;
      }

      .subtitle {
        font-size: 14px;
        color: #840ad0;
      }
    }

    .el-form-item {
      margin-bottom: 20px;
    }

    .custom-input {
      flex: 1;
      :deep(.el-input__wrapper) {
        padding: 0 16px;
        border-radius: 14px;
        background-color: rgba(255, 255, 255, 0.7);
        border: 1px solid rgba(233, 213, 255, 0.6); // ✅ 紫色边框
        box-shadow: 0 2px 8px rgba(192, 132, 252, 0.2); // ✅ 柔和阴影
        backdrop-filter: blur(5px); // ✅ 毛玻璃效果
        transition: all 0.3s ease;

        &.is-focus {
          border-color: #a855f7; // ✅ 聚焦时高亮边框
          box-shadow: 0 0 0 2px rgba(168, 85, 247, 0.3);
        }
      }

      :deep(.el-input__inner) {
        height: 46px;
        font-size: 14px;
        color: #ffffff; // ✅ 白色字体适配深色背景
        background-color: transparent; // ✅ 透明内层背景
        caret-color: #f472b6; // ✅ 光标颜色为粉色
      }
    }


    .input-icon {
      color: #c084fc;
      font-size: 18px;
    }

    .code-container {
      display: flex;
      align-items: center;
      gap: 12px;

      .code-image-wrapper {
        flex-shrink: 0;
        width: 120px;
        height: 46px;
        border-radius: 12px;
        overflow: hidden;
        cursor: pointer;
        box-shadow: 0 2px 8px rgba(192, 132, 252, 0.2);
        transition: all 0.3s;

        &:hover {
          transform: scale(1.02);
        }

        .login-code-img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }
    }

    .remember-me {
      :deep(.el-checkbox__label) {
        color: #7e6bc1;
      }
    }

    .login-btn {
      width: 100%;
      height: 46px;
      font-size: 16px;
      font-weight: bold;
      border-radius: 14px;
      background: linear-gradient(135deg, #a855f7, #f9a8d4);
      color: white;
      border: none;
      box-shadow: 0 4px 14px rgba(168, 85, 247, 0.4);
      transition: all 0.3s;

      &:hover {
        background: linear-gradient(135deg, #9333ea, #f472b6);
        transform: translateY(-1px);
      }
    }

    .link-type {
      display: inline-block;
      margin-top: 8px;
      font-size: 14px;
      color: #a855f7;
      text-align: center;
      width: 100%;
      transition: 0.2s;

      &:hover {
        text-decoration: underline;
      }
    }
  }

  .el-login-footer {
    position: fixed;
    bottom: 20px;
    width: 100%;
    text-align: center;
    color: #c084fc;
    font-size: 12px;
  }
}
</style>

